/*
 * Copyright (C) 2009 The Rockchip Android MID Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.playmodule.view;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.playmodule.MediaUtil;

/**
 * Displays a video file.  The VideoView class
 * can load images from various sources (such as resources or content
 * providers), takes care of computing its measurement from the video so that
 * it can be used in any layout manager, and provides various display options
 * such as scaling and tinting.
 */
public class VideoDisplayView extends SurfaceView {
    private static final String TAG = "VideoDisplayView";

    private SurfaceHolder mSurfaceHolder = null;
    private int mVideoWidth;
    private int mVideoHeight;
    private Context mContext;

    private static int sMaxWidth = -1;
    private static int sMaxHeight = -1;
    private MediaPlayer mMediaPlayer = null;
    private int screenSizeMode = 0;
    private MediaUtil mediaHelper;

    public VideoDisplayView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public VideoDisplayView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoDisplayView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    private void init() {
        initVideoDisplayView();
    }

    public void setMediaPlayer() {
        this.mMediaPlayer = mediaHelper.getmMediaPlayer();
        mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                setScreenMode(screenSizeMode);
            }
        });
        if (mSurfaceHolder != null) {
            mMediaPlayer.setDisplay(mSurfaceHolder);
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (mMediaPlayer != null) {
            mMediaPlayer.setDisplay(null);
        }
    }

    public void setMediaHelper(MediaUtil mediaHelper) {
        this.mediaHelper = mediaHelper;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mVideoWidth, mVideoHeight);
    }

    public void setMaxWh() {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        mVideoWidth = sMaxWidth = dm.widthPixels;
        mVideoHeight = sMaxHeight = dm.heightPixels;
    }

    public void setSmallWh() {
        mVideoWidth = sMaxWidth = 581;
        mVideoHeight = sMaxHeight = 346;
    }

    private void initVideoDisplayView() {
        setMaxWh();
        getHolder().addCallback(mSHCallback);
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
    }

    public void setFullSize() {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        mVideoWidth = sMaxWidth = dm.widthPixels;
        mVideoHeight = sMaxHeight = dm.heightPixels;
        if (getVisibility() == VISIBLE && mediaHelper.isPrepare()) {
            setScreenSizeMode(0);
        }
    }

    public void setSmallSize() {
        mVideoWidth = sMaxWidth = 581;
        mVideoHeight = sMaxHeight = 346;
        if (getVisibility() == VISIBLE) {
            setScreenSizeMode(0);
        }
    }


    SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() {
        public void surfaceChanged(SurfaceHolder holder, int format,
                                   int w, int h) {

        }

        public void surfaceCreated(SurfaceHolder holder) {
            mSurfaceHolder = holder;
            Log.e("fuck you","surfaceCreated");
            if (mMediaPlayer != null) {
                mMediaPlayer.setDisplay(mSurfaceHolder);
            }
            mediaHelper.restart();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            mSurfaceHolder = null;
        }
    };

    public void setScreenSize(int width, int height) {
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        if (width > sMaxWidth) {
            width = sMaxWidth;
        }
        if (height > sMaxHeight) {
            height = sMaxHeight;
        }
//
        getHolder().setFixedSize(sMaxWidth, sMaxHeight);
        mVideoWidth = width;
        mVideoHeight = height;
        this.requestLayout();
    }

    public int setScreenMode(int ScreenMode) {
        int maxWidth = sMaxWidth;
        int maxHeight = sMaxHeight;
        switch (ScreenMode) {
            case 0:
                if (mMediaPlayer.getVideoWidth() > maxWidth || mMediaPlayer.getVideoHeight() > maxHeight) {
                    float degree = (float) mMediaPlayer.getVideoWidth() / (float) mMediaPlayer.getVideoHeight();
                    int tmpWidth1 = maxWidth;
                    int tmpHeight1 = (int) (tmpWidth1 / degree);

                    int tmpHeight2 = maxHeight;
                    int tmpWidth2 = (int) (tmpHeight2 * degree);

                    int finalWidth = 0;
                    int finalHeight = 0;

                    if (tmpHeight1 > maxHeight && tmpWidth2 <= maxWidth) {
                        finalWidth = tmpWidth2;
                        finalHeight = tmpHeight2;
                    } else if (tmpWidth2 > maxWidth && tmpHeight1 <= maxHeight) {
                        finalWidth = tmpWidth1;
                        finalHeight = tmpHeight1;
                    } else if (tmpHeight1 <= maxHeight && tmpWidth2 <= maxWidth) {
                        if (tmpWidth1 * tmpHeight1 > tmpWidth2 * tmpHeight2) {
                            finalWidth = tmpWidth1;
                            finalHeight = tmpHeight1;
                        } else {
                            finalWidth = tmpWidth2;
                            finalHeight = tmpHeight2;
                        }
                    } else {
                        finalWidth = maxWidth;
                        finalHeight = maxHeight;
                    }
                    setScreenSize(finalWidth, finalHeight);
                } else {
                    float degree = (float) mMediaPlayer.getVideoWidth() / mMediaPlayer.getVideoHeight();
                    if (mMediaPlayer.getVideoWidth() == 1920 && (float) mMediaPlayer.getVideoHeight() == 1080) {
                        setScreenSize(maxWidth, maxHeight);
                        return 0;
                    } else if (mMediaPlayer.getVideoWidth() == 1280 && mMediaPlayer.getVideoHeight() == 736) {
                        setScreenSize(maxWidth, maxHeight);
                        return 0;
                    } else if (1.73f < degree && degree < 1.78) {
                        setScreenSize(maxWidth, maxHeight);
                        return 0;
                    } else {
                        float widthdegree = (float) maxWidth / (float) mMediaPlayer.getVideoWidth();
                        float heightdegree = (float) maxHeight / (float) mMediaPlayer.getVideoHeight();
                        if (widthdegree * mMediaPlayer.getVideoHeight() < maxHeight) {
                            setScreenSize(maxWidth, (int) (widthdegree * mMediaPlayer.getVideoHeight()));
                        } else {
                            setScreenSize((int) (heightdegree * mMediaPlayer.getVideoWidth()), maxHeight);
                        }
                    }
                }

                break;
            case 1:
                if (maxWidth * 9 > maxHeight * 16) {
                    setScreenSize(maxHeight * 16 / 9, maxHeight);
                } else {
                    setScreenSize(maxWidth, maxWidth / 16 * 9);
                }
                break;
            case 2:
                if (maxWidth * 3 > maxHeight * 4) {
                    setScreenSize(maxHeight / 3 * 4, maxHeight);
                } else {
                    setScreenSize(maxWidth, maxWidth / 4 * 3);
                }
                break;
            case 3:
                setScreenSize(maxWidth, maxHeight);
                break;
            case 4:
                if (mMediaPlayer.getVideoWidth() == 0 || mMediaPlayer.getVideoHeight() == 0) {
                    setScreenSize(maxWidth, maxHeight);
                } else {
                    setScreenSize(mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight());
                }
                break;
        }
        return 0;
    }

    private float mLeft = 0.0f;
    private float mTop = 0.0f;

    @Override
    public void layout(int l, int t, int r, int b) {
        int left = (int) mLeft;
        int top = (int) mTop;
        if (mVideoWidth <= sMaxWidth) {
            left = (sMaxWidth - mVideoWidth) / 2;
        }
        if (mVideoHeight <= sMaxHeight) {
            top = (sMaxHeight - mVideoHeight) / 2;
        }
        super.layout(left, top, mVideoWidth + left, mVideoHeight + top);
    }

    public void setScreenSizeMode(int screenSizeMode) {
        setScreenMode(screenSizeMode);
        this.screenSizeMode = screenSizeMode;
    }

    public boolean hasCreated() {
        return mSurfaceHolder != null;
    }
}
